import { createStore } from 'vuex'
import { getMsgList } from '../api/enterprise.js'

// 创建一个新的 store 实例
const store = createStore({
  state: {
    cityData : '',
    weatherData : '',
    isCollapse :false,
    tagsList:[],
    tabBarActive:'index',
    user:{},
    userMsgList:[],
    userMsgCount:0,
    enterprise:{},
    enterpriseMsgList : [],
    enterpriseMsgCount : 0
  },
  getters:{},
  mutations: {
    setEnterprise(state,data){
      state.enterprise = data
    },
    setEnterpriseMsgCount(state,data){
      state.enterpriseMsgCount = data
    },
    setEnterpriseMsgList(state,data){
      console.log(data)
      state.enterpriseMsgList = data
    },
    setUser(state,data){
      state.user = data
    },
    setAddressItem(state, data){
      state.cityData=data
    },
    // 侧边栏折叠
    handleCollapse(state, data) {
        state.isCollapse = data;
    },
    delTagsItem(state, data) {
      state.tagsList.splice(data.index, 1);
    },
    setTagsItem(state, data) {
      state.tagsList.push(data)
    },
    clearTags(state) {
      state.tagsList = []
    },
    closeTagsOther(state, data) {
      state.tagsList = data;
    },
    closeCurrentTag(state, data) {
      for (let i = 0, len = state.tagsList.length; i < len; i++) {
        const item = state.tagsList[i];
        if (item.path === data.$route.fullPath) {
          if (i < len - 1) {
            data
              .$router
              .push(state.tagsList[i + 1].path);
          } else if (i > 0) {
            data
              .$router
              .push(state.tagsList[i - 1].path);
          } else {
            data
              .$router
              .push("/");
          }
          state
            .tagsList
            .splice(i, 1);
          break;
        }
      }
    },
    setTabBarActive(state, data){
      state.tabBarActive = data
    }
  },
  actions:{
    getEnterprise:async ({commit},data)=>{
      let e = await localStorage.getItem("enterprise");
      commit("setEnterprise",JSON.parse(e))
    },
    getEnterpriseMsgList:async ({commit},data)=>{
      let data1 = await getMsgList()
      commit('setEnterpriseMsgList',data1.data)
    },
    getAddress: async ({ commit }, data)=>{
      commit('setAddressItem',data)
    },
    getTabBarActive:({ commit }, dat)=>{
      commit('setTabBarActive',data)
    }
  },
  modules:{}
})

export default store